﻿using CWCS.BLL;
using CWCS.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CWCS.UIControl.Convery
{
    public partial class FrmAddConvery : C1.Win.C1Ribbon.C1RibbonForm
    {
        public FrmAddConvery()
        {
            InitializeComponent();
        }

        private void FrmAddConvery_Load(object sender, EventArgs e)
        {

        }

        private void btnGeneral_Click(object sender, EventArgs e)
        {
            ConveryBLL bll = new ConveryBLL();
            string str_start = txtFromNum.Text.Trim();
            string str_end = txtEndNum.Text.Trim();
            string str_Db = txtDB.Text.Trim();
            string str_Length = txtLength.Text.Trim();
            if (string.IsNullOrEmpty(str_start))
            {
                MessageBox.Show("起始地址为空！");
                return;
            }
            if (string.IsNullOrEmpty(str_end))
            {
                MessageBox.Show("目标地址为空！");
                return;
            }
            if (string.IsNullOrEmpty(str_Db))
            {
                MessageBox.Show("DB块为空！");
                return;
            }
            if (string.IsNullOrEmpty(str_Length))
            {
                MessageBox.Show("DB块长度为空！");
                return;
            }
            List<WCS_OPCITEM> lst = new List<WCS_OPCITEM>();
            int x = 1;
            int y = 1;
            int width = 3;
            int height = 3;
            int a = 0;
            for (int i = int.Parse(str_start); i <= int.Parse(str_end); i++)
            {
                WCS_OPCITEM entity = new WCS_OPCITEM();
                entity.Stationno = i.ToString();
                entity.Stationtype = "0";
                entity.Itemid = i.ToString();
                entity.Itemname = i.ToString();
                entity.Groupname = "Convery";
                entity.Userid = "";
                entity.Objects = str_Db;
                entity.Signaltype = str_Length;
                entity.Value = ((i - 1001) * (int.Parse(str_Length))).ToString();//DB块值
                entity.Field1 = "";
                entity.Field2 = "";
                entity.Field3 = "";
                entity.Field4 = "";
                entity.Field5 = "";
                entity.Status = "1";
                entity.Warehouseid = "ab933387cffe489cb65b29665d0c58e5";
                entity.Locationx = x.ToString();
                entity.Locationy = (width + y).ToString();
                entity.Width = width.ToString();
                entity.Height = height.ToString();
                entity.Belong = "0";
                entity.AreaCode = "NCXWDT3-L2-FR";
                entity.ZoneCode = "FR";
                if (x % 50 == 0)
                {
                    x -= 2;
                    entity.Height =width.ToString();
                    entity.Width = height.ToString();
                    y += 2;
                }
                else
                {
                    x += 2;
                }
                if (x <= 0)
                    x = 1;
                a++;
                lst.Add(entity);
            }
            string strMsg = "";
            if (bll.InsertOPCData(lst, ref strMsg))
            {
                MessageBox.Show("批量添加成功！", "提示");
            }
            else
            {
                MessageBox.Show("批量添加失败！失败原因：" + strMsg, "提示");
            }
            //this.Close();
        }
    }
}
